package org.sjs.dreamfly.biz.history

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.sjs.dreamfly.R
import org.sjs.dreamfly.common.base.BaseViewHolder
import org.sjs.dreamfly.common.util.Router
import org.sjs.dreamfly.player.PlayerActivity
import org.sjs.dreamfly.model.HistoryItem
import org.sjs.dreamfly.common.util.PhemonUtils
import org.sjs.dreamfly.data.AppDataBase
import org.sjs.dreamfly.model.ResourceType

class HistoryItemViewHolder(itemView: View) : BaseViewHolder<HistoryItem>(itemView) {
    @SuppressLint("SetTextI18n")
    override fun bind(data: HistoryItem, position: Int) {
        itemView.findViewById<TextView>(R.id.index).let {
            if (data.resourceType == ResourceType.MOVIE) {
                if (data.movieInfo?.isSingle() == true) {
                    it.text = PhemonUtils.formatTime(data.videoProgress)
                } else {
                    it.text = "第${data.movieProgress + 1}集"
                }
            }
        }
        itemView.findViewById<ImageView>(R.id.cover).let {
            Glide.with(itemView).load(data.imageUrl).into(it)
        }
        itemView.setOnClickListener {
           if (data.resourceType == ResourceType.MOVIE) {
               MainScope().launch {
                   AppDataBase.getDatabase().movieDao().query(data.resourceId).firstOrNull()?.let {
                        Router.launchMovieDetail(it, it.channel?:"")
                   }
               }

           }
        }
    }
}
